﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Astronergy.MES.App.Library
{
    public class EmailHelper
    {
        //设置邮件发送
        private static SmtpClient _client = null;
        private static MailMessage _message = new MailMessage();

        public EmailHelper(string mailServer, string mailUserName, string mailPassword, string mailFrom, string mailSender, string mailTos, string mailCcs)
        {
            //设置邮件发送服务器
            _client = new SmtpClient(mailServer, 25);
            //设置发送人的邮箱账号和密码
            _client.Credentials = new NetworkCredential(mailUserName, mailPassword);
            //启用ssl,也就是安全发送
            _client.EnableSsl = false;

            //设置发件人,发件人需要与设置的邮件发送服务器的邮箱一致
            _message.From = new MailAddress(mailFrom, mailSender, Encoding.UTF8);
            //设置收件人
            string[] mailToArray = mailTos.Split(new char[] { ';', ',' });
            foreach (var mailTo in mailToArray)
            {
                _message.To.Add(mailTo);
            }
            //设置抄送人
            string[] mailCcArray = mailCcs.Split(new char[] { ';', ',' });
            foreach (var mailCc in mailCcArray)
            {
                _message.CC.Add(mailCc);
            }
        }

        public void SendEMail(string subject, string mailContent)
        {
            //设置邮件标题
            _message.Subject = subject;
            //设置邮件内容
            _message.Body = mailContent;

            //发送邮件
            _client.Send(_message);
        }

    }
}
